const db = require('../db/index');

/**
 * operation_person
 * operation_content
 * operation_level
 * operation_time
 */

// 操作记录
exports.operationLog = (req, res) => {
  let {
    operation_content,
    operation_level,
    operation_person
  } = req.body;
  let operation_time = new Date();
  let sql = 'insert into operation_log set ?';
  db.query(sql, {
    operation_content,
    operation_level,
    operation_person,
    operation_time
  }, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('操作记录失败');
    res.send({
      status: 0,
      message: '操作记录成功'
    })
  })
}

// 获取操作记录列表
exports.getOperateLogList = (req, res) => {
  let sql = 'select * from operation_log';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results,
      total: results.length
    })
  })
}

// 分页
exports.getOperateLogListByPage = (req, res) => {
  let {
    pageSize
  } = req.body;
  pageSize = (pageSize - 1) * 10;
  let sql = `select * from operation_log order by operation_time limit 10 offset ${pageSIze}`;
  db.query(sql, pageSize, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results,
    })
  })
}

// 清空操作记录
exports.clearOperateLog = (req, res) => {
  let sql = 'truncate table operation_log';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '清空记录成功'
    })
  })
}